<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>

        class EventEmitter {
            constructor() {
                this.subs = Object.create(null)
            }

            $on(eventType, handler ) {
                this.subs[eventType] = this.subs[eventType] || []
                this.subs[eventType].push(handler)
            }

            $emit(eventType) {
                const sub = this.subs[eventType]
                if (!sub) return
                sub.forEach(handler => {
                    handler()
                })
            }
        }

        const em = new EventEmitter()

        em.$on('message', () => {
            console.log(1111)
        })

        em.$on('message', () => {
            console.log(2222)
        })

        em.$emit('message')
    </script>
</body>
</html>